package com.zhiche.lisa.oms.service.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.google.common.collect.Lists;
import com.zhiche.lisa.core.supports.RestfulResponse;
import com.zhiche.lisa.core.utils.HttpClientUtil;
import com.zhiche.lisa.oms.dao.model.bo.ShippingInfoBO;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.List;

@Component
public class ErpUtil {

    @Value("${erp.getNewestShipByLicenseUrl}")
    private String getNewestShipByLicenseUrl;

    //连接超时时间
    @Value("${erp.socketTimeout}")
    private int socketTimeout;

    /**
     * 调用接口获取调度指令
     * @param license
     * @return
     */
    public ShippingInfoBO getNewestShipByERP(String license){
        ShippingInfoBO shippingInfoBO = null;
        // 查询发卡方类型
        List<NameValuePair> headNamePairs = Lists.newArrayList();
        headNamePairs.add(new BasicNameValuePair("Authorization",""));
        List<NameValuePair> params = Lists.newArrayList();
        params.add(new BasicNameValuePair("vclicense",license));
        String strReturn = HttpClientUtil.get(getNewestShipByLicenseUrl,headNamePairs,params,socketTimeout);
        if(!StringUtils.isEmpty(strReturn)){
            RestfulResponse<ShippingInfoBO> restVO = JSON.parseObject(strReturn, new TypeReference<RestfulResponse<ShippingInfoBO>>(){});
            shippingInfoBO = restVO.getData();
        }
        return shippingInfoBO;
    }

}
